package com.cardbaobao.cardbabyclient.ui.activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

import com.cardbaobao.cardbabyclient.R;
import com.cardbaobao.cardbabyclient.api.Api;
import com.cardbaobao.cardbabyclient.base.Constant;
import com.cardbaobao.cardbabyclient.buried.Self;
import com.cardbaobao.cardbabyclient.model.QrCodeDebitFeeRateData;
import com.cardbaobao.cardbabyclient.model.QuickDebitFeeRateData;
import com.cardbaobao.cardbabyclient.model.result.QrCodeDebitFeeRateResult;
import com.cardbaobao.cardbabyclient.model.result.QuickDebitFeeRateResult;
import com.cardbaobao.cardbabyclient.ui.activity.base.BaseActivity;
import com.cardbaobao.cardbabyclient.utils.ApplicationHelper;
import com.cardbaobao.cardbabyclient.buried.GoogleAnalyticsUtil;
import com.cardbaobao.cardbabyclient.utils.JsonUtil;
import com.cardbaobao.cardbabyclient.utils.ToastUtil;
import com.cardbaobao.cardbabyclient.widget.TitleBar;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.OnClick;
import rx.Subscriber;

/**
 * Created by Jiangzx on 2017/9/16 14:56.
 */

public class BindCardSuccessActivity extends BaseActivity {
    public static final String NEED_BIND_CREDIT_CARD_B="needBindCreditCard";
    @BindView(R.id.title_bar)
    TitleBar titleBar;

    @BindView(R.id.bt_debit_now)
    Button btDebitNow;
    int type;

    boolean needBindCreditCard;
    String name;
    String idCardNumber;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_bind_card_success;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        titleBar.setLeftViewOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        Intent intent = getIntent();

        mCurrentPageId= Self.Page.app_authentication_success_addcard;

        type = intent.getIntExtra(DebitIntroActivity.DEBIT_TYPE_I,
                DebitIntroActivity.QUICK_DEBIT);
        needBindCreditCard = intent.getBooleanExtra(NEED_BIND_CREDIT_CARD_B, false);
        name = intent.getStringExtra(AddCreditCardActivity.NAME_S);
        idCardNumber = intent.getStringExtra(AddCreditCardActivity.ID_CARD_NO_S);
        if (type == DebitIntroActivity.QUICK_DEBIT) {
            if (needBindCreditCard) {
                btDebitNow.setText("添加信用卡");
            } else {
                btDebitNow.setText("立即收款");
            }
        } else {
            btDebitNow.setText("立即收款");
        }
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        super.initData(savedInstanceState);
    }

    @Override
    protected void onResume() {
        super.onResume();
        GoogleAnalyticsUtil.setScreenName("储蓄卡添加成功");
    }

    @OnClick(R.id.bt_debit_now)
    public void onClick(View view){
        switch (view.getId()){
            case R.id.bt_debit_now:
                if(type==DebitIntroActivity.QUICK_DEBIT){
                    //快捷收款，如果没绑信用卡，需要去绑定信用卡，否则去到收款页面
                    if(needBindCreditCard){
                        //去绑定信用卡
                        Intent intent=new Intent(mContext, AddCreditCardActivity.class);
                        intent.putExtra(AddCreditCardActivity.NAME_S,name);
                        intent.putExtra(AddCreditCardActivity.ID_CARD_NO_S,idCardNumber);
                        intent.putExtra(AddCreditCardActivity.PAGE_FROM_I,
                                AddCreditCardActivity.PAGE_QUICK_DEBIT);
                        intent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
                        startActivity(intent);
                        finish();
                    }else{
                        getQuickDebitFeeRates();
                    }
                }else if(type==DebitIntroActivity.QR_CODE_DEBIT){
                    //二维码收款
                    getQrCodeDebitFeeRates();
                }
                break;
        }
    }

    //获取快捷收款 收款费率
    private void getQuickDebitFeeRates() {
        showProgressDialog(false);
        Api.getQuickDebitFeeRate().subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                        getString(R.string.string_request_data_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                QuickDebitFeeRateResult result = JsonUtil.json2Object(s, QuickDebitFeeRateResult.class);
                if (result.getCode() == Constant.SUC_CODE) {
                    QuickDebitFeeRateData data = result.getData();
                    if (data != null) {
                        ArrayList<QuickDebitFeeRateData.FeeRate> rates = data.getRate_list();
                        if (rates != null && rates.size() > 0) {
                            boolean canQuickDebit = false;
                            for (QuickDebitFeeRateData.FeeRate r : rates) {
                                if (Constant.KQB010101.equals(r.getChannel_type())) {
                                    canQuickDebit = true;
                                    break;
                                }
                            }
                            if (canQuickDebit) {
                                Intent intent = new Intent();
                                intent.putParcelableArrayListExtra(FeeRateActivity.QUICK_DEBIT_FEE_RATE_LIST_ARRAY,
                                        rates);
                                intent.putExtra(FeeRateActivity.FEE_TYPE_I,
                                        FeeRateActivity.QUICK_DEBIT_FEE_TYPE);
                                intent.setClass(mContext, QuickDebitActivity.class);
                                intent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
                                startActivity(intent);
                                finish();
                            } else {
                                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                        getString(R.string.string_data_exception));
                            }
                        }
                    }
                } else {
                    ToastUtil.showToast(result.getMsg());
                }
            }
        });
    }

    //获取二维码收款 收款费率
    private void getQrCodeDebitFeeRates() {
        showProgressDialog(false);
        Api.getQrCodeDebitFeeRate().subscribe(new Subscriber<String>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable throwable) {
                dismissProgressDialog();
                ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                        getString(R.string.string_request_data_fail));
            }

            @Override
            public void onNext(String s) {
                dismissProgressDialog();
                QrCodeDebitFeeRateResult result =
                        JsonUtil.json2Object(s, QrCodeDebitFeeRateResult.class);
                if (result.getCode() == Constant.SUC_CODE) {
                    QrCodeDebitFeeRateData data = result.getData();
                    if (data != null) {
                        ArrayList<QrCodeDebitFeeRateData.FeeRate> rates = data.getRateList();
                        if (rates != null && rates.size() > 0) {
                            Intent intent = new Intent();
                            intent.setClass(mContext, QrCodeDebitActivity.class);
                            intent.putExtra(FeeRateActivity.FEE_TYPE_I,
                                    FeeRateActivity.QR_CODE_DEBIT_FEE_TYPE);
                            intent.putExtra(FeeRateActivity.QR_CODE_DEBIT_FEE_RATE_LIST_ARRAY,
                                    rates);
                            intent.putExtra(PRE_PAGE_ID_S,mCurrentPageId);
                            startActivity(intent);
                            finish();
                        } else {
                            ToastUtil.showToast(ApplicationHelper.getApplicationContext().
                                    getString(R.string.string_data_exception));
                        }
                    }
                } else {
                    ToastUtil.showToast(result.getMsg());
                }
            }
        });
    }
}
